<template>
    <div class="maori-product-instance-dialog-container">
        <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
            <el-form ref="maoriProductInstanceDialogFormRef" :model="state.ruleForm" size="default" label-width="80px">
                <el-row :gutter="35">

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="state.productInfo.variantCodeOne">
                            <el-input v-model="state.ruleForm.variantCodeOneValue" clearable></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col v-if="state.btTwoExist" :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="state.productInfo.variantCodeTwo">
                            <el-input v-model="state.ruleForm.variantCodeTwoValue" clearable></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="产品-长">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.ruleForm.productLength" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="产品-宽">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.ruleForm.productWidth" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="产品-高">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.ruleForm.productHeight" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="装箱量">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.ruleForm.outerCartonPackCount" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">个</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="装箱-长">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.ruleForm.packetLength" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="装箱-宽">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.ruleForm.packetWidth" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="装箱-高">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.ruleForm.packetHeight" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="单个产品净重">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.ruleForm.productWeight" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.weightUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="单个产品包装重">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.ruleForm.packageWeight" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.weightUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="整箱实重">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.ruleForm.packetTrueWeight" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.weightUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel" size="default">取 消</el-button>
                    <el-button type="primary" @click="addInstance" size="default">{{ state.dialog.submitTxt
                        }}</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts" name="maoriProductInstanceDialog">
import { reactive, ref } from 'vue';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['addInstance']);

// 定义变量内容
const maoriProductInstanceDialogFormRef = ref();
const state = reactive({
    ruleForm: {
        variantCodeOneValue: '',
        variantCodeTwoValue: '',
        productLength: '',          //产品-长
        productWidth: '',           //产品-宽
        productHeight: '',          //产品-高
        packetLength: '',            //装箱-长
        packetWidth: '',             //装箱-宽
        packetHeight: '',            //装箱-高
        outerCartonPackCount: '',   //装箱量
        productWeight: '',          //单个产品净重
        packageWeight: '',          //单个产品包装重
        packetTrueWeight: '',          //整箱实重
    },
    productInfo: {
        variantCodeOne: '变体1',
        variantCodeTwo: '变体2',
        priceUnit: '$' as any,
        productUnit: 'CM',
        weightUnit: 'KG',
    },
    btTwoExist: false,
    dialog: {
        isShowDialog: false,
        type: '',
        title: '',
        submitTxt: '',
    },
});
// 打开弹窗
const openDialog = (productInfo: CostingProduct) => {
    state.dialog.title = '新增产品单项';
    state.dialog.submitTxt = '确 定';

    state.productInfo = productInfo;

    if ('' != productInfo.variantCodeTwo) {
        state.btTwoExist = true;
    }
    state.productInfo.priceUnit = productInfo.priceUnit;

    state.dialog.type = 'add';
    state.dialog.isShowDialog = true;
    state.ruleForm = {
        variantCodeOneValue: '',
        variantCodeTwoValue: '',
        productLength: '',          //产品-长
        productWidth: '',           //产品-宽
        productHeight: '',          //产品-高
        packetLength: '',            //装箱-长
        packetWidth: '',             //装箱-宽
        packetHeight: '',            //装箱-高
        outerCartonPackCount: '',   //装箱量
        productWeight: '',          //单个产品净重
        packageWeight: '',          //单个产品包装重
        packetTrueWeight: '',          //整箱实重
    }
};
// 关闭弹窗
const closeDialog = () => {
    state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
    closeDialog();
};

// 提交
async function addInstance() {
    closeDialog(); // 关闭弹窗
    const instance = state.ruleForm;
    emit('addInstance', instance);
};
// 暴露变量
defineExpose({
    openDialog,
});
</script>
